home *** CD-ROM | disk | FTP | other *** search
/ CD Concept 6 / CD Concept 06.iso / mac / UTILITAIRE / RLaB / rlib / printmat.r < prev    next >
Text File  |  1994-09-23  |  3KB  |  133 lines

  1. //--------------------------------------------------------------------------------
  2. // printmat
  3.  
  4. // Synopsis: Print a matrix with title and labels.
  5.  
  6. // Syntax: printmat(a,anme,rlab,clab);
  7.  
  8. // Description:
  9.  
  10. // This routine prints the matrix a using the title contained in the
  11. // string name and the row labels contained in rowlab and the
  12. // column labels contained in collab. Note: rowlab and collab are
  13. // vectors of strings, such as
  14. //
  15. //         rowlab[1]="alpha";
  16. //         rowlab[2]="beta";
  17. //         rowlab[3]="gamma";
  18. //
  19. //  Note: name, rowlab, and collab are optional variables.
  20. //
  21. //--------------------------------------------------------------------------------
  22.  
  23. printmat = function (a, mname, rowlab, collab, fn)
  24. {
  25.  
  26.   narg=0;
  27.   if (!exist (a)) { error ("printmat: must supply a matrix"); }
  28.   if (!exist (mname)) { mname = ""; }
  29.   if (!exist (rowlab)) { rl = []; else rl = rowlab; }
  30.   if (!exist (collab)) { cl = []; else cl = collab; }
  31.   if (!exist (fn))     { fn = "stdout"; }
  32.   
  33.   nrows=a.nr;
  34.   ncols=a.nc;
  35.   
  36.   // Create row and column labels if necessary
  37.   if (rl.n == 0)
  38.   {
  39.     for (i in 1:nrows) 
  40.     {
  41.       sprintf (tmp, "%3i", i);
  42.       rl[i]="--"+ tmp +" --> ";
  43.     }
  44.   }
  45.   if (cl.n == 0)
  46.   {   
  47.     for (i in 1:ncols) { cl[i]="----"+int2str(i)+"---- "; }
  48.   }
  49.   
  50.   col_per_scrn=5;
  51.   len=12;
  52.   
  53.   if ((nrows==0)||(ncols==0)) 
  54.   { 
  55.     if (length (mname)) 
  56.     {
  57.       fprintf(fn," \n%s = \n \n",mname);
  58.       return 0;
  59.     }
  60.     fprintf(fn," \n%s \n","     [] \n");
  61.     return 0;
  62.   }
  63.  
  64.   // Print matrix name
  65.   col=1;
  66.   n = min([col_per_scrn-1,ncols-1]);
  67.   if (length (mname)) 
  68.   {
  69.     fprintf(fn,"\n%s = \n \n",mname);
  70.   }
  71.  
  72.   // Print column labels
  73.   s="";
  74.   icol=0;
  75.   while (col <= ncols) 
  76.   {
  77.     icol=icol+1;
  78.     s="            ";
  79.     for (j in 0:n) 
  80.     {
  81.       ishift=12-length(cl[j+col]);
  82.       for (k in 1:ishift) 
  83.       {
  84.     s=s+" ";
  85.       }
  86.       s=s+cl[j+col];
  87.     }
  88.     fprintf(fn,"%s\n",s);
  89.     
  90.     // Print Row Labels
  91.     for (i in 1:nrows) 
  92.     {
  93.       s=""+rl[i];
  94.       ishift=12-length(rl[i]);
  95.       for (k in 1:ishift) 
  96.       {
  97.     s=s+" ";
  98.       }
  99.       for (j in 0:n) 
  100.       {
  101.     element = a[i;col+j];
  102.     if (element == 0) {
  103.       s=s+"           0";
  104.     else if (element >= 1.0e+06) {
  105.       sdum="";
  106.       sprintf(sdum,"%12.5e",element);
  107.       s=s+sdum;
  108.     else if (element <= -1.0e+05) {
  109.       sdum="";
  110.       sprintf(sdum,"%12.5e",element);
  111.       s=s+sdum;
  112.     else if (abs(element) < 0.0001) {
  113.       sdum="";
  114.       sprintf(sdum,"%12.5e",element);
  115.       s=s+sdum;
  116.     else
  117.       sdum="";
  118.       sprintf(sdum,"%12.5f",element);
  119.       s=s+sdum;
  120.         } } } }
  121.       }
  122.       fprintf(fn,"%s\n",s);
  123.     }
  124.     col = col+col_per_scrn;
  125.     fprintf(fn,"%s"," \n");
  126.     if ((ncols-col) < n) 
  127.     {
  128.       n=ncols-col;
  129.     }
  130.   }
  131.   return 0;  
  132. };
  133.